package com.ea.controller.front;

import com.ea.api.controller.user.UserInfoApi;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.domain.dto.LoginDTO;
import com.ea.domain.dto.UserCreateDTO;
import com.ea.domain.dto.UserInfoDTO;
import com.ea.domain.dto.UserUpdateDTO;
import com.ea.domain.entity.UserInfo;
import com.ea.holder.UserHolderInfo;
import com.ea.service.IUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 电器商城
 * @since 2022-11-29
 */
@RestController
@RequestMapping("/electric/front/user")
public class UserInfoController implements UserInfoApi {

    @Autowired
    private IUserInfoService userInfoService;


    @Override
    @PostMapping("/loginIn")
    public JsonResult userLoginIn(@RequestBody LoginDTO loginDTO) {
        if (StringUtils.isEmpty(loginDTO.getLoginAccount()) || StringUtils.isEmpty(loginDTO.getLoginPassword())) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        UserInfoDTO userInfoDTO = userInfoService.loginIn(loginDTO);
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("userInfo", userInfoDTO);
        returnMap.put("token", userInfoDTO.getToken());
        return JsonResult.loginSuccessFul(returnMap);
    }

    @Override
    @PostMapping("/loginOut")
    public JsonResult userLoginOut() {
        Long userId = UserHolderInfo.getUserId();
        userInfoService.userLoginOut(userId);
        return JsonResult.loginOut();
    }

    @Override
    @PostMapping("/register")
    public JsonResult userRegister(UserCreateDTO createDTO) {
        if (createDTO == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        userInfoService.userRegister(createDTO);
        return JsonResult.doSuccessful();
    }

    @Override
    @PostMapping("/write/off")
    public JsonResult userWriteOff() {
        Long userId = UserHolderInfo.getUserId();
        if (userId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        userInfoService.writeOffUser(userId);
        return JsonResult.delSuccessFul();
    }

    @Override
    @PostMapping("/info")
    public JsonResult getUserInfo() {
        Long userId = UserHolderInfo.getUserId();
        if (userId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return JsonResult.doSuccessful(userInfo);
    }

    @Override
    @PostMapping("/update")
    public JsonResult updateUserInfo(@RequestBody UserUpdateDTO updateDTO) {
        if (updateDTO == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        userInfoService.updateUserInfo(updateDTO);
        return JsonResult.updateSuccessFul();
    }

    @Override
    @PostMapping("/change")
    public JsonResult changePassword(Long userId, String oldPassword, String newPassword) {
        if (userId == null || StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        userInfoService.changePassword(userId, oldPassword, newPassword);
        return JsonResult.updateSuccessFul();
    }
}
